<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    #box {
      float: left;
      width: 400px;
      height: 300px;
      border: 1px solid #000;
    }

    #box img {
      width: 100%;
      height: 100%;
    }

    #box1 {
      float: left;
      width: 300px;
      height: 300px;
      border: 1px solid #000;
      margin-left: 50px;
      overflow: hidden;
      display: none;
      position: relative;
    }

    #box1 img {
      position: absolute;
      left: 0;
      top: 0;
    }

    #lay {
      display: none;
      background-color: orange;
      opacity: 0.5;
      position: absolute;
      left: 0;
      top: 0;
      cursor: move;
    }
  </style>
  <script>
    window.onload = function () {
      var box = document.getElementById('box');
      var box1 = document.getElementById('box1');
      var Img = box1.getElementsByTagName('img')[0];
      // 获取黄色图标
      var lay = document.getElementById('lay');
      box.onmouseenter = function () {
        box1.style.display = 'block';
        lay.style.display = 'block';

      }
      box.onmouseleave = function () {
        box1.style.display = 'none';
        lay.style.display = 'none';
      }
      box.onmousemove = function (e) {
        var e = e || event;
        // 图片缩放比列
        var scale = 4;
        var x = e.pageX - this.offsetLeft - lay.offsetWidth / 2;
        var y = e.pageY - this.offsetTop - lay.offsetHeight / 2;
        // 黄色图片高宽
        lay.style.width = parseInt(this.offsetWidth / scale) + 'px';
        lay.style.height = parseInt(this.offsetHeight / scale) + 'px'
        // 大图片放大
        Img.style.width = this.offsetWidth * scale + 'px';
        Img.style.height = this.offsetHeight * scale + 'px';
        // 黄色图片移动
        if (x < 0) {
          x = 0;
        }
        if (x >= this.offsetWidth - lay.offsetWidth) {
          x = this.offsetWidth - lay.offsetWidth;
        }
        if (y < 0) {
          y = 0;
        }
        if (y >= this.offsetHeight - lay.offsetHeight) {
          y = this.offsetHeight - lay.offsetHeight;
        }
        lay.style.left = x + 'px';
        lay.style.top = y + 'px';
        // 大图片移动
        var left = lay.offsetLeft * scale;
        var top = lay.offsetTop * scale;
        Img.style.left = -left + 'px';
        Img.style.top = -top + 'px';
      }
    }
  </script>
</head>

<body>
  <div id="box">
    <img src="./images/1.jpeg" alt="">
    <div id="lay"></div>
  </div>
  <div id="box1">
    <img src="./images/1.jpeg" alt="">
  </div>
</body>

</html>